package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CskaoyanmallGroupon;
import com.cskaoyan.bean.CskaoyanmallGrouponExample;
import com.cskaoyan.bean.CskaoyanmallGrouponRules;
import com.cskaoyan.bean.CskaoyanmallGrouponRulesExample;
import com.cskaoyan.bean.bo.ExtensionGrouponRulesBo;
import com.cskaoyan.bean.vo.GrouponRulesVo;
import com.cskaoyan.mapper.CskaoyanmallGrouponMapper;
import com.cskaoyan.mapper.CskaoyanmallGrouponRulesMapper;
import com.cskaoyan.service.ExtensionGrouponRulesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class ExtensionGrouponRulesServiceImpl implements ExtensionGrouponRulesService {
    @Autowired
    CskaoyanmallGrouponRulesMapper cskaoyanmallGrouponRulesMapper;

        @Override
        public GrouponRulesVo vagueQueryGrouponRules(Integer page, Integer limit, String sort, String order, Integer goodsId) {

            PageHelper.startPage(page, limit);
            CskaoyanmallGrouponRulesExample grouponRulesExample = new CskaoyanmallGrouponRulesExample();
            grouponRulesExample.setOrderByClause(sort + " " + order);

            CskaoyanmallGrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();
            if (goodsId != null) {
                criteria.andGoodsIdEqualTo(goodsId);
            }

            List<CskaoyanmallGrouponRules> grouponRules = cskaoyanmallGrouponRulesMapper.selectByExample(grouponRulesExample);

            Iterator<CskaoyanmallGrouponRules> iterator = grouponRules.iterator();
            while (iterator.hasNext()) {
                if (iterator.next().getDeleted()) {
                    iterator.remove();
                }
            }


            PageInfo<CskaoyanmallGrouponRules> grouponRulesPageInfo = new PageInfo<>(grouponRules);
            long total = grouponRulesPageInfo.getTotal();

            GrouponRulesVo grouponRulesVo = new GrouponRulesVo();
            grouponRulesVo.setItems(grouponRules);
            grouponRulesVo.setTotal(total);

            return grouponRulesVo;
        }

    @Override
    public Integer deleteGroupRule(CskaoyanmallGrouponRules grouponRulesBo) {

        grouponRulesBo.setDeleted(true);
        Integer i = cskaoyanmallGrouponRulesMapper.updateByPrimaryKey(grouponRulesBo);

        return i;
    }

    @Override
    public Integer updateGrouponRule(ExtensionGrouponRulesBo grouponRulesBo) {

//        做一下自定义BO到逆向工程提供BO的转换
        CskaoyanmallGrouponRules cskaoyanmallGrouponRules = new CskaoyanmallGrouponRules();
        cskaoyanmallGrouponRules.setId(grouponRulesBo.getId());
        cskaoyanmallGrouponRules.setGoodsId(Integer.valueOf(grouponRulesBo.getGoodsId()));
        cskaoyanmallGrouponRules.setGoodsName(grouponRulesBo.getGoodsName());
        cskaoyanmallGrouponRules.setPicUrl(grouponRulesBo.getPicUrl());
        cskaoyanmallGrouponRules.setDiscount(new BigDecimal(grouponRulesBo.getDiscount()));
        cskaoyanmallGrouponRules.setDiscountMember(Integer.valueOf(grouponRulesBo.getDiscountMember()));
        cskaoyanmallGrouponRules.setDeleted(grouponRulesBo.getDeleted());
//        String---->Date
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date parse = sf.parse(grouponRulesBo.getAddTime());
            cskaoyanmallGrouponRules.setAddTime(sf.parse(grouponRulesBo.getAddTime()));
            cskaoyanmallGrouponRules.setUpdateTime(sf.parse(grouponRulesBo.getUpdateTime()));
            cskaoyanmallGrouponRules.setExpireTime(sf.parse(grouponRulesBo.getExpireTime()));

        } catch (ParseException e) {
            e.printStackTrace();
        }


        Integer i = cskaoyanmallGrouponRulesMapper.updateByPrimaryKey(cskaoyanmallGrouponRules);

        return i;
    }
}
